package vfc.views.results
{
	//import assets.Assets;
	
	import flexlib.containers.ButtonScrollingCanvas;
	
	import mate.events.SiteEvent;
	
	import mx.collections.ArrayCollection;
	import mx.containers.VBox;
	import mx.controls.Image;
	
	import vfc.model.valueObjects.Site;

	public class ResultsList extends ButtonScrollingCanvas
	{
		
		protected var _dataProvider:ArrayCollection;
		protected var _selectedItem:Site;
		protected var _selectedIndex:int;
		private var dataProviderChanged:Boolean;
		private var listItemContainer:VBox;
		
		public function ResultsList()
		{
			super();
			addEventListener(SiteEvent.SITE_SELECTED,onListItemSelected);
		}
		
		override protected function commitProperties():void{
			super.commitProperties();
			if(dataProviderChanged){
				listItemContainer.removeAllChildren();
				dataProviderChanged = false;
				createResultsList();
				selectedIndex = 0;
			}
		}
		
		override protected function createChildren():void{
			super.createChildren();
			
			var backgroundImage:Image = new Image();
			backgroundImage.includeInLayout = false;
			backgroundImage.percentHeight = 100;
			backgroundImage.percentWidth = 100;
			backgroundImage.alpha = .6;
			addChild(backgroundImage);
			
			listItemContainer = new VBox();
			listItemContainer.setStyle("verticalGap",1);
			listItemContainer.percentHeight = 100;
			listItemContainer.percentWidth = 100;
			addChild(listItemContainer);
		}
		
		private function createResultsList():void{
			removeAllChildren();
				
			var ct:int=0;
			for each(var site:Site in _dataProvider){
				var listItem:ListItem = new ListItem();
				listItem.site = site;
				listItem.index = ct;
				ct++;
				listItemContainer.addChild(listItem);
			}
			
		}
		
		private function onListItemSelected(event:SiteEvent):void{
			selectedIndex = event.target.index;
			selectedItem = event.target.site;
		}
		
		public function get selectedItem():Site {
			return _selectedItem;
		}

		public function set selectedItem(o:Site):void {
			_selectedItem = o;
		}
		
		public function get selectedIndex():int {
			return _selectedIndex;
		}

		public function set selectedIndex(o:int):void {
			if(_dataProvider && _dataProvider.length>0){
				//disable previous
				ListItem(listItemContainer.getChildAt(_selectedIndex)).selected = false;
				
				_selectedIndex = o;
				ListItem(listItemContainer.getChildAt(_selectedIndex)).selected = true;
			}
		}

		[Bindable]
		public function get dataProvider():ArrayCollection {
			return _dataProvider;
		}

		public function set dataProvider(o:ArrayCollection):void {
			_dataProvider = o;
			dataProviderChanged = true;
			invalidateProperties();
		}
		
		override protected function updateDisplayList(unscaledWidth:Number,unscaledHeight:Number):void
		{
			super.updateDisplayList(unscaledWidth,unscaledHeight);
			if(!listItemContainer)
				return;
			
			var listItems:Array = listItemContainer.getChildren();
			for each(var listItem:ListItem in listItems)
			{
				listItem.width = width;
				//listItem.height = listItem.measuredHeight;
			}
		}
	}
}